package com.kmist.xiakexing.view;


import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.kmist.xiakexing.R;
import com.kmist.xiakexing.adapter.CommonAdapter;
import com.kmist.xiakexing.interfaces.ITaskCallback;
import com.kmist.xiakexing.presenters.PlayerPresenter;
import com.kmist.xiakexing.presenters.TaskPresenter;
import com.kmist.xiakexing.tools.DialogRe;
import com.kmist.xiakexing.utils.scene.PlayerTaskBean;
import com.kmist.xiakexing.utils.scene.TaskBean;

import java.util.ArrayList;
import java.util.List;

public class TaskInfoDialog extends DialogRe implements View.OnClickListener, ITaskCallback {

    private TextView task_info;
    private TextView task_name;
    private Button task_btn_ack;
    private Button task_btn_cancle;
    private TaskPresenter taskPresenter;
    private PlayerPresenter playerPresenter;
    private TaskBean curTask;
    private PlayerTaskBean curPlayerTask;;
    private String taskDialogue;

    public TaskInfoDialog(Context context) {
        super(context, R.layout.a_login_create_new_player);

    }

    public TaskInfoDialog(Context context, TaskBean taskBean) {
        super(context, R.layout.task_info_dialog);
        taskPresenter = TaskPresenter.getInstance();
        playerPresenter = PlayerPresenter.getInstance();
        curTask = taskBean;
        curPlayerTask = taskPresenter.getPlayerTask(playerPresenter.getPlayerd().getSid(), taskBean.getTaskId());

        Init();
        task_name = mWindow.findViewById(R.id.task_dialog_name);
        task_info = mWindow.findViewById(R.id.task_dialog_info);
        task_btn_ack = mWindow.findViewById(R.id.task_btn_accept);
        task_btn_cancle = mWindow.findViewById(R.id.task_btn_cancle);

        task_btn_ack.setOnClickListener(this);
        task_btn_cancle.setOnClickListener(this);

        show_task_name(taskBean.getTaskName()); /* 显示任务名称 */
        show_task_dialogure(taskBean.getTaskInfo()); /* 显示任务的对白 */
        show_task_button(); /* 调控button显示文字 */
    }

    private void show_task_button() {
        if(curPlayerTask == null){
            task_btn_ack.setText("领取任务");
            return;
        }

        if(curPlayerTask.getTaskStatus() == 2){
            task_btn_ack.setText("提交任务");
            return;
        }
    }

    private void show_task_name(String taskName) {
        task_name.setText(taskName);   /* 任务名称 */
    }

    private void show_task_dialogure(String info) {
        int taskStatus = 0;
        if(curPlayerTask != null){
            taskStatus = curPlayerTask.getTaskStatus();
        }
        task_info.setText(taskPresenter.getTaskDialogue(taskStatus, info)); /* 任务信息 */
    }

    public void sureEvent(){
        super.sureEvent();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.task_btn_accept:
                String sid = playerPresenter.getPlayerd().getSid();

                // 接受任务
                if(curPlayerTask == null){
                    AcceptTask(sid);
                }else if(curPlayerTask != null && curPlayerTask.getTaskStatus() == 2){
                    AccepdTask(sid);
                }
                break;
            case R.id.task_btn_cancle:
                close();
                break;
        }
    }

    /* 提交任务动作 */
    private void AccepdTask(String sid) {
        //提交任务
        if(curPlayerTask.getTaskId() == curTask.getTaskId() && curPlayerTask.getTaskStatus() == 2)
        {
            if(curPlayerTask.getTaskNowCount() >= curPlayerTask.getTaskCount() || curPlayerTask.getTaskType() == 3){
                taskPresenter.taskStatusOver(sid, curPlayerTask.getTaskId());
                taskDialogue =taskPresenter.getTaskDialogue(3, curTask.getTaskInfo());
                task_info.setText(taskDialogue); /* 任务信息 接受任务后说的话 */
                show_task_button_none();
            }
        }
    }

    /* 接受任务动作 */
    private void AcceptTask(String sid) {
        taskPresenter.acceptTask(sid, curTask.getTaskId());
        curPlayerTask = taskPresenter.getPlayerTask(playerPresenter.getPlayerd().getSid(), curTask.getTaskId());

        //如果是对话类的任务就在接受任务后，就把任务更新成 2， 已完成。
        if( curPlayerTask != null && curPlayerTask.getTaskType() == 3){
            taskPresenter.updateTaskStatus(sid, curTask.getTaskId(), 2);
            taskDialogue = taskPresenter.getTaskDialogue(1, curTask.getTaskInfo());
            if( taskDialogue == ""){
                close();
            }else{
                task_info.setText(taskDialogue); /* 任务信息 接受任务后说的话 */
                show_task_button_none();
            }
        }else{
            task_info.setText("请不要重复接取任务");
        }
    }

    private void show_task_button_none() {
        task_btn_ack.setVisibility(View.INVISIBLE);
        task_btn_cancle.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onTaskLoaded(TaskBean task) {
        //TODO

    }

    @Override
    public void onTaskLoaded(List<TaskBean> tasks) {
        //NOTODO
    }

    @Override
    public void onTaskLoadEmpty() {
        //TODO
    }

    @Override
    public void onTaskLoadError() {
        //TODO
    }
}
